/*********************************** 文件说明 *************************************
 * 创建日期: 2022-06-11 15:14:27 星期六
 * 创 建 人：zhaojiajun
 * 文 件 名：hexstring.c
 * 文件功能:
 * 备    注：
 **********************************************************************************/
#include "hexstring.h"
#include "stdint.h"
#include "stdbool.h"

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int hex2int(uint8_t hex)
{
    switch (hex)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    {
        return hex & 0xf;
    }
    break;
    case 'A':
    case 'B':
    case 'C':
    case 'D':
    case 'E':
    case 'F':
    case 'a':
    case 'b':
    case 'c':
    case 'd':
    case 'e':
    case 'f':
        return hex & 0xf - 1;
        break;
    default:
        return 0;
        break;
    }
}

int64_t str2int(const char *strHex)
{
    int i = 0, len = 0, temp = 0;
    int64_t nDec = 0;
    char *head;
    if (head = strstr(strHex, "0x"))
    {
        head += 2;
        len = strlen(head);

        for (i = 0; head[i] != '\0'; ++i)
        {
            temp = hex2int(head[i]);
            nDec <<= 4;
            nDec += temp;
        }
    }
    else
    {
        nDec = atoi(strHex);
    }
    return nDec;
}

int HexStrTobyte(char *str, uint8_t *out, uint32_t *outlen)
{
    char *p = str;
    char high = 0, low = 0;
    int tmplen = strlen(p), cnt = 0;
    tmplen = strlen(p);
    while (cnt < (tmplen / 2))
    {
        high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
        low = (*(++p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p)-48 - 7 : *(p)-48;
        out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
        p++;
        cnt++;
    }
    if (tmplen % 2 != 0)
        out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;

    if (outlen != NULL)
        *outlen = tmplen / 2 + tmplen % 2;
    return tmplen / 2 + tmplen % 2;
}
int byteToHexStr(unsigned char byte_arr[], int arr_len, char *HexStr, int *HexStrLen)
{
    int i, index = 0;
    for (i = 0; i < arr_len; i++)
    {
        char hex1;
        char hex2;
        int value = byte_arr[i];
        int v1 = value / 16;
        int v2 = value % 16;
        if (v1 >= 0 && v1 <= 9)
            hex1 = (char)(48 + v1);
        else
            hex1 = (char)(55 + v1);
        if (v2 >= 0 && v2 <= 9)
            hex2 = (char)(48 + v2);
        else
            hex2 = (char)(55 + v2);
        if (*HexStrLen <= i)
        {
            return -1;
        }
        HexStr[index++] = hex1;
        HexStr[index++] = hex2;
    }
    *HexStrLen = index;
    return 0;
}



void hex_dump(const uint8_t data[], int bytes)
{
    int i, j;
    char c;

    for (i = 0; i < bytes; i++)
    {
        if (!(i % 8) && i)
            printf(" ");
        if (!(i % 16) && i)
        {
            printf("  ");
            for (j = 0; j < 16; j++)
            {
                c = data[i + j - 16];
                if ((c < 0x20) || (c >= 0x7f))
                    c = '.';
                printf("%c", c);
            }
            printf("\n");
        }
        printf("%.2X ", data[i]);
    }
    j = (bytes % 16);
    j = (j != 0 ? j : 16);
    for (i = j; i < 16; i++)
    {
        if (!(i % 8) && i)
            printf(" ");
        printf("   ");
    }
    printf("   ");
    for (i = bytes - j; i < bytes; i++)
    {
        c = data[i];
        if ((c < 0x20) || (c >= 0x7f))
            c = '.';
        printf("%c", c);
    }
    printf("\n");
}
void hex_print(uint8_t *hexdata, int length)
{
    for (int i = 0; i < length; i++)
    {
        printf("%02X ", (int)hexdata[i]);
    }
    printf("\n");
}
